package com.blb.controller;

import com.blb.entity.Categories;
import com.blb.entity.Product;
import com.blb.entity.Relation;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.ICategoriesService;
import com.blb.service.IRelationService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-15
 */
@Controller
@RequestMapping("/relation")
public class RelationController {
    @Resource
   private IRelationService relationService;

    @Resource
    private ICategoriesService categoriesService;

    /**
     * 首页的分页列表
     * @param companyProvince
     * @param companyCity
     * @param companyArea
     * @param companyName
     * @param start
     * @param length
     * @return
     * xuyuanxin
     */
    @ResponseBody
    @PostMapping("page")
    public BlbPage page(String companyProvince, String companyCity, String companyArea, String companyName, int start, int length){
        User user = ShiroUtils.getUser();

        List<Relation> relations = relationService.SupplierCompany(start,length,user.getCompanyId(), companyProvince, companyCity, companyArea, companyName);

        int count = relationService.count(user.getCompanyId(),companyProvince, companyCity, companyArea, companyName);

        return new DataTablePage(count, count, relations);
    }


    /**
     * 点击企业名称，企业的详细信息
     * @param model
     * @return
     * xuyuanxin
     */
    @RequestMapping("detailsupplier")
    public String findBySupplierId(Model model){
        User user = ShiroUtils.getUser();
        Relation allrelation=relationService.findByCompanyId(user.getCompanyId());
        String companyId=allrelation.getCompany().getId();
        System.out.println(companyId);
        model.addAttribute("companyId",companyId);
        model.addAttribute("relations",allrelation);
        return "supplier/supplier";
    }

    /**
     * 企业备注修改
     * @param rRemark
     * @param companyId
     * xuyuanxin
     */
    @ResponseBody
    @RequestMapping("updateCompany")
    public ResponseHelp updateCompany(String rRemark, String companyId){
        relationService.updateCompany(rRemark, companyId);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;
    }

    /**
     * 企业对应产品的详细信息（三级菜单）
     * @param nodeId
     * @param companyId
     * @param start
     * @param length
     * @return
     * xuyuanxin
     */
    @ResponseBody
    @RequestMapping("productPage")
    public BlbPage page(String nodeId, String companyId, int start, int length){

        List<Categories> allNode = categoriesService.getAllNode(nodeId, companyId);
        System.out.println(allNode);
        //如果他为最小的子节点，没有父id
        List<String> sIds=new ArrayList<>();

        if(allNode.isEmpty()){
            sIds.add(nodeId);
            List<Product> scp = relationService.getSupplierCompanyProductInfo(sIds, companyId, start, length);
            int count = relationService.count1(sIds,companyId);
            return new DataTablePage(count, count, scp);
        }

        List<String> ids=new ArrayList<>();
        for(Categories c:allNode){
            getChildrens(c,ids);
        }
        //将二级菜单的id塞到集合里
        ids.add(nodeId);

        List<Product> supplierCompanyProductInfo = relationService.getSupplierCompanyProductInfo(ids, companyId, start, length);

        int count = relationService.count1(ids,companyId);

        return new DataTablePage(count, count, supplierCompanyProductInfo);
    }

    public void getChildrens(Categories categories, List<String> list){
        list.add(categories.getId());
        if (categories.getProducts()!=null) {
            for (Categories c : categories.getProducts()) {
                getChildrens(c, list);
            }
        }
    }
}

